﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;


public class GameManger : MonoBehaviour
{
    private float x; //用来存储随机数
    private float z;
    public List<Transform> enemySpawnPos;//存储敌人出生点位置
    public GameObject[] enemyPrefab;
    public GameObject empiricalBallPrefabs;//经验球预制体
    public GameObject BloodPrefabs; //存放血包
    public Transform empiricalBallParent;//用来存放生成的经验球
    public Transform enemyParent; //用来存放生成的敌人在场景中的位置


    public GameObject[] character = new GameObject[] { };
    public Player play;

    public List<GameObject> enemyList;
    public Image selectMenu;
    SortedList<string, int> rankingList =new SortedList<string, int>();


    private static GameManger _Instance;
    public static GameManger Insatance { get { return _Instance; } }
    public  List<string> nameList = new List<string>() { "小红", "小黄", "提比", "九七", "小芶", "小葱", "小仪", "小桢", "Megumin", "tippy", "97797" };
    void Awake()
    {
        
        if (_Instance != null)
        {
            Destroy(this.gameObject); return;
        }
        _Instance = this;
        //生成经验球
        for (int i = 0; i < 770; i++)
        {
            x = Random.Range(-40, 40);
            z = Random.Range(-40, 40);
            Vector3 ballPos = new Vector3(x, 0.15f, z);
            Instantiate(empiricalBallPrefabs, ballPos, transform.rotation, empiricalBallParent);
        }
        for (int i = 0; i < 170; i++)
        {
            x = Random.Range(-30, 30);
            z = Random.Range(-30, 30);
            Vector3 ballPos = new Vector3(x, 0.15f, z);
            Instantiate(BloodPrefabs, ballPos, transform.rotation, empiricalBallParent);
        }
        StartCoroutine("spawnEnemy");
        //开启协程
        character = GameObject.FindGameObjectsWithTag("Enemy");
        play = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();
        
    }
    public struct listTemp
    {
        public int fsl;
        public string namel;
        public listTemp(int fs, string nz)
        {
            this.fsl = fs;
            this.namel = nz;
        }
    }
    public void Update()
    {
        rankingList.Clear();
        foreach(GameObject temp in character)
        {
            int s = temp.GetComponent<Enemy>().score;
            string e = temp.GetComponent<Enemy>().EnemyName;
           rankingList.Add(e, s);
        }
        rankingList.Add(play.PlayerName, play.score);
        #region 排序
        if (Time.frameCount % 30 == 0)
        {
            List<listTemp> sxb = new List<listTemp>();

            for (int i = 0; i < rankingList.Count; i++)
            {
                sxb.Add(new listTemp(rankingList.Values[i], rankingList.Keys[i]));
            }


            for (int i = 0; i < sxb.Count - 1; i++)
            {
                for (int j = 0; j < sxb.Count - 1 - i; j++)
                {
                    if (sxb[j].fsl < sxb[j + 1].fsl)
                    {
                        listTemp temp = sxb[j];
                        sxb[j] = new listTemp(sxb[j + 1].fsl, sxb[j + 1].namel);
                        sxb[j + 1] = temp;
                    }
                }
            }

            for (int i = 0; i < 10; i++)
            {
                leaderBoard.Insatance.leaderboardName[i].GetComponent<Text>().text = sxb[i].namel;
                leaderBoard.Insatance.leaderboardScore[i].GetComponent<Text>().text = sxb[i].fsl.ToString();
            }
        }
        #endregion



    }

    IEnumerator spawnEnemy()//生成敌人
    {
        foreach (Transform temp in enemySpawnPos)
        {
            int r = Random.Range(0, enemyPrefab.Length);
            enemyList.Add(Instantiate(enemyPrefab[r], temp.position, temp.rotation));
        }

        //int r = Random.Range(0, enemyPrefab.Length);
        //enemyList.Add(Instantiate(enemyPrefab[r], enemySpawnPos[0].position, enemySpawnPos[0].rotation)) ;
        yield break;
    }


    /// <summary>
    /// 游戏结束
    /// </summary>
    public void GameOver()
    {
        Application.LoadLevel("EndMenu");
    }


    /// <summary>
    /// 返回开始菜单
    /// </summary>

}







